package sensorSelectionFramework;


public class SelectionControl {
	private Sensor sensor = null;
	private OptimalSensorsSet optimalSensorsSet= new OptimalSensorsSet();
	private SensorSet sensorSet = null;

	public void setSensor(Sensor sensor) {
		this.sensor = sensor;
	}

	public Sensor getSensor() {
		return sensor;
	}

	public void setOptimalSensorsSet(OptimalSensorsSet optimalSensorsSet) {
		this.optimalSensorsSet = optimalSensorsSet;
	}

	public SensorSet getOptimalSensorsSet() {
		return optimalSensorsSet;
	}

	public void setSensorSet(SensorSet sensorSet) {
		this.sensorSet = sensorSet;
	}

	public SensorSet getSensorSet() {
		return sensorSet;
	}
	
	public void ramdomSelection(SensorSet inputSet){
		int totalCost = 0;
		for(Sensor sensor:inputSet.getSensorList())
		{
			totalCost += sensor.getCost();
			if(totalCost <= optimalSensorsSet.getBudget())
			{
				optimalSensorsSet.sensorList.add(sensor);
			}
			else
			{
				totalCost = totalCost-sensor.getCost();
			}
			
		}
		
	}
	public void greedySelection(SensorSet inputSet){
		int totalCost = 0;
		Sensor tempSensor = null;
		while(totalCost <= optimalSensorsSet.getBudget()& inputSet.getSensorList().size()>0)
		{
			int maxValue = 0;
			for(Sensor sensor:inputSet.getSensorList())
			{
				if(maxValue < sensor.getValue())
				{
					maxValue = sensor.getValue();
					tempSensor = sensor;
				}
			}
			if (totalCost+tempSensor.getCost() <= optimalSensorsSet.getBudget())
			{
				totalCost += tempSensor.getCost();
				optimalSensorsSet.getSensorList().add(tempSensor);
			}
			inputSet.getSensorList().remove(tempSensor);
		}
		
	}
	
	public void dynamicSelection(SensorSet inputSet)
	{
		int budget = optimalSensorsSet.getBudget();
		int sensorCount = inputSet.getSensorList().size();
		int sensorCost[] = new int[sensorCount];
		int sensorPrice[] = new int[sensorCount];
		
		for (int i = 0; i< sensorCost.length;i++)
		{
			sensorCost[i] = inputSet.getSensorList().get(i).getCost();
			sensorPrice[i] = inputSet.getSensorList().get(i).getValue();
		}
		
		int selectionMatrix [][] = new int [sensorCount+1][budget+1];
		//initialize the matrix 
		for (int j = 0; j<sensorCount;j++ )
		{
			for (int k = 0; k< budget; k++)
			{
				selectionMatrix[j][k]=0;
			}
		}
		
		// fill the dynamic selection matrix
		
		for (int tempSensorCount = 1; tempSensorCount <= sensorCount; tempSensorCount ++)
		{
			for (int tempBudget = 1; tempBudget <= budget; tempBudget ++)
			{
				if (sensorCost[tempSensorCount-1] <= tempBudget)
				{
					// if (current sensor's value + value of corresponding value with complement budget) > current value
					if (sensorPrice[tempSensorCount-1] + selectionMatrix[tempSensorCount-1][tempBudget - sensorCost[tempSensorCount-1]]
					                                                                    > selectionMatrix[tempSensorCount-1][tempBudget])
					{//tempSensorCount-1 to make sure it starts at 0 
						selectionMatrix[tempSensorCount][tempBudget]= sensorPrice[tempSensorCount-1] + selectionMatrix[tempSensorCount-1][tempBudget - sensorCost[tempSensorCount-1]];
					}
					else
					{
						selectionMatrix[tempSensorCount][tempBudget]= selectionMatrix[tempSensorCount-1][tempBudget];
					}
				}
				else
				{
					selectionMatrix[tempSensorCount][tempBudget]= selectionMatrix[tempSensorCount-1][tempBudget];
				}
			}
		}
		
		// pick the elements for optimal set from selection matrix 
		
		for (int sensorNumber = sensorCount; sensorNumber >=1 ; sensorNumber--)
		{
			int tempIter = budget;
			// if there's only one sensor and its cost is less than budget
			if(sensorCount == 1 & sensorCost[sensorCount-1]<budget)
			{
				optimalSensorsSet.getSensorList().add(inputSet.getSensorList().get(0));
			}
			//more than one sensor
			else
			{
				
				while (selectionMatrix[sensorNumber][tempIter] == selectionMatrix[sensorNumber][tempIter - 1]& tempIter > 1)
				{
					tempIter -- ;	
				}
				if (tempIter > 1)
				{
					//if selectionMatrix[sensorNumber][tempIter]== selectionMatrix[sensorNumber-1][tempIter], it means current sensor never been selected.
					if(selectionMatrix[sensorNumber][tempIter]!= selectionMatrix[sensorNumber-1][tempIter])
					{
						optimalSensorsSet.getSensorList().add(inputSet.getSensorList().get(sensorNumber-1)); 
						budget = budget - sensorCost[sensorNumber-1];
					}
				}
			}
		}
	}
}
